regionTableLine = 0;
groupTableLine = 0;
regionMemberList = null;
groupList = null;
currentGroup = "";
groupdata = null;
regionMemberIsReady = false;
groupMemberIsReady = false;
deleteIDList = null;
addIdList = null;
function checkIsDel(userId) {
    if (deleteIDList == null) {
        return false
    }
    for (var i = 0; i < deleteIDList.length; i++) {
        if (userId === deleteIDList[i][0]) {
            return true
        }
    }
    return false
}
function checkIsAdd(userId) {
    if (null == addIdList) {
        return false
    }
    for (var i = 0; i < addIdList.length; i++) {
        if (userId === addIdList[i][0]) {
            return true
        }
    }
    return false
}
function addToGroup(userId, userName) {
    if (addIdList == null) {
        addIdList = []
    }
    if (checkIsDel(userId)) {
        for (var i = 0; i < deleteIDList.length; i++) {
            if (userId === deleteIDList[i][0]) {
                deleteIDList.splice(i, 1);
                break
            }
        }
    } else {
        if (!checkIsAdd(userId)) {
            addIdList.push(new Array(userId, userName))
        }
    }
    createRegionMemberTable();
    createGroupMemberTable()
}
function gam_addToGroup(index) {
    if ("" === $(".groupList2").val()) {
        alert("请选择群组");
        return
    }
    var i = 0;
    var userId = "";
    var userName = "";
    $("#table1 tr").each(function() {
        if (i == index) {
            var tr = $(this);
            var tds = tr.find("td");
            var j = 0;
            tds.each(function() {
                if (j == 0) {
                    var td = $(this);
                    userId = td.text()
                }
                if (j == 1) {
                    var td = $(this);
                    userName = td.text()
                }
                j++
            });
            return false
        }
        i++
    });
    if ("" === userId) {
        return
    }
    addToGroup(userId, userName)
}
function gam_groupDeleteMem(index) {
    var i = 0;
    var userId = "";
    var userName = "";
    $("#table2 tr").each(function() {
        if (i == index) {
            var tr = $(this);
            var tds = tr.find("td");
            var j = 0;
            tds.each(function() {
                if (j == 0) {
                    var td = $(this);
                    userId = td.text()
                }
                if (j == 1) {
                    var td = $(this);
                    userName = td.text()
                }
                j++
            });
            return false
        }
        i++
    });
    if ("" === userId) {
        return
    }
    deleteFromGroup(userId, userName)
}
function deleteFromGroup(userId, userName) {
    if (null == deleteIDList) {
        deleteIDList = new Array()
    }
    if (checkIsAdd(userId)) {
        for (var i = 0; i < addIdList.length; i++) {
            if (userId === addIdList[i][0]) {
                addIdList.splice(i, 1);
                break
            }
        }
    } else {
        if (!checkIsDel(userId)) {
            deleteIDList.push(new Array(userId, userName))
        }
    }
    createRegionMemberTable();
    createGroupMemberTable()
}
function gdm_getRegionMemberList() {
    if (null == regionMemberList) {
        var httpRequest = createXmlHttpRequest();
        var regionId = getCookie("region_id");
        var keyList = ["&mod=", "&ac=", "&regionID="];
        var valueList = ["region", "SeekRegionMem", regionId];
        makeRequest(keyList, valueList, httpRequest,
        function() {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                var b = httpRequest.responseText;
                var res = eval("(" + b + ")");
                if (res.Result == 0) {
                    regionMemberList = res;
                    regionMemberIsReady = true;
                    gam_showData()
                }
            }
        })
    }
}
function gdm_getGroupList() {
    if (null == groupList) {
        var httpRequest = createXmlHttpRequest();
        var regionId = getCookie("region_id");
        var keyList = ["&mod=", "&ac=", "&RegionID="];
        var valueList = ["region", "SeekRegion", regionId];
        makeRequest(keyList, valueList, httpRequest,
        function() {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                var b = httpRequest.responseText;
                var res = eval("(" + b + ")");
                if (res.Result == 0) {
                    groupList = res;
                    var groupIdList = res.Groups.split(",");
                    for (var i = 0; i < res.GroupName.length; i++) {
                        $(".groupList2").append("<option value='" + groupIdList[i] + "'>" + res.GroupName[i] + "</option>")
                    }
                }
            }
        })
    }
}


function gam_checkGroup() {
    var groupId = $(".groupList2").val();
    gdm_getGroupMember(groupId)
}
function gdm_getGroupMember(groupId) {
    if (currentGroup === groupId) {
        return
    }
    if ("" != groupId) {
        if (null != deleteIDList) {
            delete deleteIDList;
            deleteIDList = new Array()
        }
        if (null != addIdList) {
            delete addIdList;
            addIdList = new Array()
        }
        currentGroup = groupId;
        var httpRequest = createXmlHttpRequest();
        var keyList = ["&mod=", "&ac=", "&GroupID="];
        var valueList = ["Group", "GetGroupAllMember", groupId];
        groupMemberIsReady = false;
        makeRequest(keyList, valueList, httpRequest,
        function() {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                var b = httpRequest.responseText;
                var res = eval("(" + b + ")");
                if (res.Result == 0) {
                    groupdata = res;
                    groupMemberIsReady = true;
                    gam_showData()
                }
            }
        })
    }
}


function gam_showData() {
    if (regionMemberIsReady) {
        if (currentGroup === "") {
            createRegionMemberTable();
            createGroupMemberTable()
        } else {
            if (groupMemberIsReady) {
                createRegionMemberTable();
                createGroupMemberTable()
            }
        }
    }
}
function gam_show(groupId) {
    gdm_getGroupList();
    gdm_getGroupMember(groupId);
    gdm_getRegionMemberList()

}
function gam_checkIsGroupMem(userId) {
    if (!groupMemberIsReady) {
        return false
    }
    for (var i = 0; i < groupdata.Count; i++) {
        if (groupdata.MemberIDs[i].UserID === userId) {
            return true
        }
    }
    return false
}
function createRegionMemberTable() {
    var index = 0;
    var table = $("#tb_left");
    group_deleteTable("tb_left", regionTableLine);
    for (var i = 0; i < regionMemberList.Number; i++) {
        if (checkIsAdd(regionMemberList.MemberList[i].UserID)) {
            continue
        }
        if (gam_checkIsGroupMem(regionMemberList.MemberList[i].UserID)) {
            continue
        }
        var tr = $('<tr class="table_tr">			<td>' + regionMemberList.MemberList[i].UserID + "</td>			<td>" + regionMemberList.MemberList[i].UserName + '</td>			<td><a href="#" onclick="gam_addToGroup(' + index + ')" style="text-decoration:none;">加入群组</a></td>		</tr>');
        tr.appendTo(table);
        index++
    }
    if (null != deleteIDList) {
        for (var j = 0; j < deleteIDList.length; j++) {
            var tr = $('<tr class="table_tr">				<td>' + deleteIDList[j][0] + "</td>				<td>" + deleteIDList[j][1] + '</td>				<td><a href="#" onclick="gam_addToGroup(' + index + ')" style="text-decoration:none;">加入群组</a></td>			</tr>');
            tr.appendTo(table);
            index++
        }
        if (null != addIdList) {
            regionTableLine = regionMemberList.Number + deleteIDList.length - addIdList.length
        } else {
            regionTableLine = regionMemberList.Number + deleteIDList.length
        }
    } else {
        regionTableLine = regionMemberList.Number
    }
}
function createGroupMemberTable() {
    if ("" == currentGroup) {
        return
    } else {
        var index = 0;
        group_deleteTable("tb_right", groupTableLine);
        var table = $("#tb_right");
        for (var i = 0; i < groupdata.Count; i++) {
            if (checkIsDel(groupdata.MemberIDs[i].UserID)) {
                continue
            }
            var tr = $('<tr class="table_tr">				<td> '+ groupdata.MemberIDs[i].UserID +' </td>				<td> '+ groupdata.MemberIDs[i].UserName + '</td>				<td><a href="#" onclick="gam_groupDeleteMem(' + index + ')" style="text-decoration:none;">删除</a></td>			</tr>');
            tr.appendTo(table);
            index++
        }
        if (null != addIdList) {
            for (var j = 0; j < addIdList.length; j++) {
                var tr = $('<tr class="table_tr">					<td>' + addIdList[j][0] + "</td>					<td>" + addIdList[j][1] + '</td>					<td><a href="#" onclick="gam_groupDeleteMem(' + index + ')" style="text-decoration:none;">删除</a></td>				</tr>');
                tr.appendTo(table);
                index++
            }
            if (deleteIDList != null) {
                groupTableLine = groupdata.Count + addIdList.length - deleteIDList.length
            } else {
                groupTableLine = groupdata.Count + addIdList.length
            }
        } else {
            groupTableLine = groupdata.Count
        }
    }
}
function group_deleteTable(name, line) {
    for (var i = 0; i < line; i++) {
        $("#" + name + " tr:last").remove()
    }
}
function gam_commitData() {
    if (null == deleteIDList && null == addIdList) {
        alert("没有增删任何成员");
        return
    }
    var nomember = true;
    if (null != deleteIDList) {
        if (deleteIDList.length > 0) {
            nomember = false
        }
    }
    if (null != addIdList) {
        if (addIdList.length > 0) {
            nomember = false
        }
    }
    if (nomember) {
        alert("没有增删任何成员");
        return
    }
    var groupId = $(".groupList2").val();
    if (groupId == "") {
        alert("请选择群组");
        return
    }
    var httpRequest = createXmlHttpRequest();
    var regionId = getCookie("region_id");
    var keyList = ["&mod=", "&ac=", "&GroupID="];
    var valueList = ["region", "GroupModifyMember", groupId];
    if (null != addIdList) {
        if (addIdList.length > 0) {
            var addStr = addIdList[0][0];
            for (var i = 1; i < addIdList.length; i++) {
                addStr = addStr + "," + addIdList[i][0]
            }
            keyList.push("&AddIDList=");
            valueList.push(addStr)
        }
    }
    if (null != deleteIDList) {
        if (deleteIDList.length > 0) {
            var addStr = deleteIDList[0][0];
            for (var i = 1; i < deleteIDList.length; i++) {
                addStr = addStr + "," + deleteIDList[i][0]
            }
            keyList.push("&DeleteIDList=");
            valueList.push(addStr)
        }
    }
    makeRequest(keyList, valueList, httpRequest,
    function() {
        if (httpRequest.readyState == 4 && httpRequest.status == 200) {
            var b = httpRequest.responseText;
            var res = eval("(" + b + ")");
            if (res.Result == 0) {
                alert("操作成功");
                deleteIDList = null;
                addIdList = null;
                var groupId = $(".groupList2").val();
                gam_show(groupId)
            } else {
                errorParse(res.Result)
            }
        }
    })
};